//
//  WelfareActivityCenterProgressView.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/10.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "WelfareActivityCenterProgressView.h"

static const float imgBtnWidth = 10;

@interface WelfareActivityCenterProgressView ()

@property (nonatomic,strong) UIProgressView *progressView;

//用UIButton防止以后有点击事件 (圆点)
@property (nonatomic,strong) NSMutableArray *imgBtnArray;

@property (strong, nonatomic) NSMutableArray *iconArr;

@end

@implementation WelfareActivityCenterProgressView

+ (instancetype)progressViewFrame:(CGRect)frame withImageArray:(NSArray *)imageArray
{
    WelfareActivityCenterProgressView *stepProgressView = [[WelfareActivityCenterProgressView alloc] initWithFrame:frame];
    //进度条
    stepProgressView.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, frame.size.height - imgBtnWidth + 3, frame.size.width, 2)];
    stepProgressView.progressView.progressViewStyle = UIProgressViewStyleBar;
    stepProgressView.progressView.transform = CGAffineTransformMakeScale(1.0f,2.0f);
    stepProgressView.progressView.progressTintColor = Colorff0000;
    stepProgressView.progressView.trackTintColor = Color999999;
//    stepProgressView.progressView.progress = 1;
    stepProgressView.progressView.layer.cornerRadius = 5;
    stepProgressView.progressView.layer.masksToBounds = YES;

    [stepProgressView addSubview:stepProgressView.progressView];
    
    stepProgressView.imgBtnArray = [[NSMutableArray alloc] init];
    stepProgressView.iconArr = [[NSMutableArray alloc] init];
    
    float _btnWidth = frame.size.width / (imageArray.count);
    for (int i = 0; i < imageArray.count; i++) {

        //图片按钮
        UIImage *normalImage = [UIImage createImageWithColor:Color999999 withWidth:imgBtnWidth];
        UIImage *selectedImage = [UIImage createImageWithColor:Colorff0000 withWidth:imgBtnWidth];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:normalImage forState:UIControlStateNormal];
        [btn setImage:selectedImage forState:UIControlStateSelected];

        btn.frame = CGRectMake(_btnWidth + _btnWidth * i - imgBtnWidth, frame.size.height - imgBtnWidth, imgBtnWidth, imgBtnWidth);
        btn.selected = YES;
        
        btn.layer.cornerRadius =  imgBtnWidth / 2.0;
        btn.layer.masksToBounds = YES;
        
        [stepProgressView addSubview:btn];
        [stepProgressView.imgBtnArray addObject:btn];
        
        // 图片
        UIButton *imageBtn = [[UIButton alloc] init];
        imageBtn.tag = i;
        imageBtn.frame = CGRectMake((_btnWidth - jisuan(100)) /2.0 + _btnWidth * i, 0, jisuan(100), jisuan(70));
        NSDictionary *dict = imageArray[i];
        [imageBtn setImage:[UIImage imageNamed:dict[@"normal"]] forState:UIControlStateNormal];
        [imageBtn setImage:[UIImage imageNamed:dict[@"selected"]] forState:UIControlStateSelected];
        [imageBtn addTarget:stepProgressView action:@selector(imageBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
        
        [stepProgressView addSubview:imageBtn];
        [stepProgressView.iconArr addObject:imageBtn];

    }
    
    stepProgressView.stepIndex = 0;
 
    return stepProgressView;
    
}

#pragma mark -奖励领取
- (void)imageBtnCLick:(UIButton *)btn {

    if (btn.selected) {
        
        NSLog(@"领取奖励");
        
        return;
        
    }else {
        
        NSLog(@"尚未达到标准");
        
        return;
    }
 
}


-(void)setStepIndex:(CGFloat)stepIndex
{

    _stepIndex = stepIndex >= (NSInteger)(_imgBtnArray.count - 1) ? _imgBtnArray.count - 1 : stepIndex;
 
    NSArray *progressArr = @[@"0.25", @"0.5", @"0.75", @"1.0"];
    
    for (int i = 0; i < _imgBtnArray.count; i++) {
        
        CGFloat progressValue = [progressArr[i] floatValue];
        
        UIButton *btn = [_imgBtnArray objectAtIndex:i];
        UIButton *iconBtn = [_iconArr objectAtIndex:i];
        
        if (progressValue <= _stepIndex ) {
            
            btn.selected = YES;
            iconBtn.selected = YES;
        
        }
        else{
            
            btn.selected = NO;
            iconBtn.selected = NO;
        }
        
    }
    
    if (_stepIndex == 0) {
        
        self.progressView.progress = 0.0;
    }
    else if (_stepIndex == 1.0)
    {
        self.progressView.progress = 1.0;
    }
    else
    {

        self.progressView.progress = _stepIndex;
    }
}

@end
